/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 053. 二叉搜索树中的中序后继
 * @author: WXY
 * @create: 2023-01-04 19:40
 * @Version 1.0
 **/
public class offer_eighteen1_inorderSuccessor {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (root == null) return null;
        if (root.val <= p.val) {
            return inorderSuccessor(root.right, p);
        }
        TreeNode node = inorderSuccessor(root.left, p);
        return node == null ? root : node;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
